<?php
include_once '../icloud_files/system/DB.php';


class Pagination extends DB {

    public function p($value){
        if(!isset($_POST[$value])) {
            return false;
        }
        return $_POST[$value];
    }
/* ------------------------------------------------------------------------------------------------- */


    //pagination - indexes
    public function p_index() {
        if($this->p("type") || ($this->p("page") && $this->p("howMuch"))) {
            include '../icloud_files/models/AdvModel.php';
            $adv = new AdvModel();

            //check if AJAX request comes from cars or parts search
            if(strpos($_SERVER['HTTP_REFERER'], 'index_parts')) {
                $part = true;
                $adv->search_adv_parts($_SESSION['last_search'], $this->p("price"), $this->p("date")); //pass 2 params - price and date for sort
            } else {
                $part = false;
                $adv->search_adv($_SESSION['last_search'], $this->p("price"), $this->p("date"));
            }

            if($this->p("type") == "getPageLength") {
                echo count($adv->adv_list); //output total results number
            } else { //output relevant page results
                if(count($adv->messages) > 0) {
                    echo $adv->messages[0]; //output error
                } else {
                    //instantiate a user model to get if seller is dealer
                    include '../icloud_files/models/UserModel.php';
                    $user = new UserModel();
                    //echo "<pre>" . print_r($adv, true) . "</pre>";exit;
                    $starting_number = ($this->p("page")-1) * $this->p("howMuch");
                    $ending_number = $this->p("howMuch") * $this->p("page");
                    for($i = $starting_number; $i < $ending_number; $i++) {
                        if(isset($adv->adv_list[$i])) {
                            $user->get_seller($adv->adv_list[$i]['seller_id']);
                            echo '
                            <div class="result1">
                                <div class="'.($i%2 ? 'mini_header_lighter' : 'mini_header_darker').'">
                                    <h2>Stock ID #'.$adv->adv_list[$i]["adv_id"].' '.$adv->adv_list[$i]["make"].' '.$adv->adv_list[$i]["model"].'</h2>
                                    <div class="car_info_search">
                                        <div class="dealer">
                                            <h3>'.($user->user_data['account_type'] == 'dealer' ? 'Dealer' : '').'</h3>
                                        </div>
                                        <div class="car_price">
                                            <h3>'.($_SESSION['is_logged'] ? $_SESSION['user_data']['currency'].number_format($adv->adv_list[$i]["price"] *
                                                $_SESSION['user_data']['currency_rate'], 0, ".", " ") : '₦ '.number_format($adv->adv_list[$i]["price"], 0, ".", " ")).'</h3>
                                        </div>
                                    </div>
                                </div>
                                <div class="results_innder_info">
                                    <div class="photo_of_car">
                                        <img src="'.$adv->adv_list[$i]["picture1"].'" alt="" />
                                    </div>
                                    <div class="car_info_search1">
                                        <p>'.((strlen($adv->adv_list[$i]["description"]) > 630) ? substr($adv->adv_list[$i]["description"], 0, 630) . '...' : $adv->adv_list[$i]["description"]).'</p>
                                        <div class="more_info_search_button">
                                            <a href="'.
                                            ($part ? "?p=adv_view&type=part&adv_id=".$adv->adv_list[$i]["adv_id"] :
                                                "?p=adv_view&type=car&adv_id=".$adv->adv_list[$i]["adv_id"] ).'">Stock Details</a>
                                        </div>
                                    </div>
                                </div>
                            </div>';
                        }
                    }
                }
            }
        }
    }
/* ------------------------------------------------------------------------------------------------- */


    //pagination - manage ads
    public function p_adv_manage() {
        if($this->p("type") || ($this->p("page") && $this->p("howMuch"))) {
            include '../icloud_files/models/AdvModel.php';
            $adv = new AdvModel();
            $adv->get_all_user_ads($_SESSION['user_data']['user_id'], $this->p("price"), $this->p("date")); //pass 2 params - price and date for sort

            //var_dump ($this->p("price"));exit;

            if($this->p("type") == "getPageLength") {
                echo count($adv->adv_list); //output total results number
            } else { //output relevant page results
                if(count($adv->messages) > 0) {
                    echo $adv->messages[0]; //output error
                } else {
                    $starting_number = ($this->p("page")-1) * $this->p("howMuch");
                    $ending_number = $this->p("howMuch") * $this->p("page");
                    for($i = $starting_number; $i < $ending_number; $i++) {
                        if(isset($adv->adv_list[$i])) {
                            echo '
                            <div class="result1">
                                <div class="'.($i%2 ? 'mini_header_lighter' : 'mini_header_darker').'">
                                    <h2>'.($adv->adv_list[$i]["is_part"] ? 'Part ID #' : 'Stock ID #').$adv->adv_list[$i]["adv_id"].' '.$adv->adv_list[$i]["make"].' '.$adv->adv_list[$i]["model"].'</h2>
                                    <div class="car_info_search">
                                        <div class="dealer">
                                            <h3>'.($adv->adv_list[$i]["is_active"] ? "Active" : "Inactive - Waiting Approval" ).'</h3>
                                        </div>
                                        <div class="car_price">
                                            <h3>'.$_SESSION['user_data']['currency'].number_format($adv->adv_list[$i]["price"] *
                                                $_SESSION['user_data']['currency_rate'], 0, ".", " ").'</h3>
                                        </div>
                                    </div>
                                </div>
                                <div class="results_innder_info">
                                    <div class="photo_of_car">
                                        <img src="'.$adv->adv_list[$i]["picture1"].'" alt="" />
                                    </div>
                                    <div class="car_info_search1">
                                        <p>'.((strlen($adv->adv_list[$i]["description"]) > 630) ? substr($adv->adv_list[$i]["description"], 0, 630) . '...' : $adv->adv_list[$i]["description"]).'</p>
                                    </div>
                                    <div class="manage_my_ads_button_holder">
                                        <div class="edit_ad">
                                            <a href="'.
                                            ($adv->adv_list[$i]["is_part"] ? "?p=adv_edit_parts&adv_id=".$adv->adv_list[$i]["adv_id"] :
                                                "?p=adv_edit&adv_id=".$adv->adv_list[$i]["adv_id"] ).'">Edit</a>
                                        </div>
                                        <div class="remove_ad adv_delete_conf">
                                            <a onClick="javascript:return confirm(\'Are you sure you want to delete this Ad? This action cannot be undone.\');" href="'.
                                ($adv->adv_list[$i]["is_part"] ? "?p=adv_manage&type=part&rm_id=".$adv->adv_list[$i]["adv_id"] :
                                    "?p=adv_manage&type=car&rm_id=".$adv->adv_list[$i]["adv_id"] ).'">Remove</a>
                                        </div>
                                    </div>
                                </div>
                            </div>';
                        }
                    }
                }
            }
        }
    }
/* ------------------------------------------------------------------------------------------------- */


    //AJAX Requests - dynamic method calling via $_POST['method']
    public function ajax_change_currency() {
        if($this->p('curr')) {
            $new_currency = stripslashes($this->p('curr'));
            $_SESSION['user_data']['currency'] = $new_currency;

            include '../icloud_files/models/UserModel.php';
            $user = new UserModel();

            $_SESSION['user_data']['currency_rate'] = $user->get_currency_rate($_SESSION['user_data']['currency']);
        }
    }
/* ------------------------------------------------------------------------------------------------- */


    public function ajax_adv_approve() {
        if($_SESSION['user_data']['account_type'] === 'admin') {
            include '../icloud_files/models/AdvModel.php';
            $adv = new AdvModel();

            $adv_id = $adv->db_connection->real_escape_string($this->p('adv_id'));
            $adv_type = $adv->db_connection->real_escape_string($this->p('adv_type'));

            if($adv_type == 'car') {
                $table = 'advertises_cars';
            } else {
                $table = 'advertises_parts';
            }

            $result = $adv->db_connection->query("UPDATE $table SET is_active=IF(is_active=1, 0, 1)
                WHERE adv_id={$adv_id}");

            if($adv->db_connection->affected_rows == 1) {
            } else {
                echo 'There has been a problem with your request, please try again later.';
            }
        } else {
            echo 'There has been a problem with your request, please try again later.';
        }
    }
/* ------------------------------------------------------------------------------------------------- */


    public function ajax_adv_delete() {
        if($_SESSION['user_data']['account_type'] === 'admin') {
            include '../icloud_files/models/AdvModel.php';
            $adv = new AdvModel();

            $adv_id = $adv->db_connection->real_escape_string($this->p('adv_id'));
            $adv_type = $adv->db_connection->real_escape_string($this->p('adv_type'));
            $user_id = (int)($this->p('user_id'));

            if($adv_type == 'car') {
                $table = 'advertises_cars';
            } else {
                $table = 'advertises_parts';
            }

            //delete adv from table
            $adv->db_connection->query("DELETE FROM $table WHERE adv_id={$adv_id}");

            //delete files and folders from server
            $adv->delete_pics($adv_id, $user_id, $adv_type);

            if($adv->db_connection->affected_rows == 1) {
                echo 'The Stock has been deleted successfully.';
            } else {
                echo 'There has been a problem with your request, please try again later.';
            }
        } else {
            echo 'There has been a problem with your request, please try again later.';
        }
    }
/* ------------------------------------------------------------------------------------------------- */


    public function ajax_pic_delete() {
        if($this->p('adv_id') && $this->p('adv_type') && $this->p('pic_id')) {
            include '../icloud_files/models/AdvModel.php';
            $adv = new AdvModel();

            $adv_id = (int)$this->p('adv_id');
            $pic = 'picture'.(int)$this->p('pic_id');
            $adv_type = $adv->db_connection->real_escape_string($this->p('adv_type'));

            if($adv_type == 'car') {
                $table = 'advertises_cars';
            } else {
                $table = 'advertises_parts';
            }

            $adv->db_connection->query("UPDATE $table SET $pic='' WHERE adv_id={$adv_id}");
        }
    }
}
